package org.example.work2.tcc.account.controller;

import io.seata.rm.tcc.api.BusinessActionContext;
import lombok.extern.slf4j.Slf4j;
import org.example.work2.tcc.account.dto.Account;
import org.example.work2.tcc.account.service.AccountAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/account")
public class AccountController {
	
	@Autowired
    private AccountAction accountAction;
	
	/**
	 *    提供查询验证账户信息
	 * @param accountCode
	 * @return
	 */
	@GetMapping("/findAccountByCode/{accountCode}")
	public Account findAccountByCode(@PathVariable("accountCode") String accountCode) {
		System.out.println("订单服务发起远程调用查询用户信息");
		Account account= accountAction.findAccountByCode(accountCode);
		System.out.println("查询结果account="+account);
		return account;
    }

	@PutMapping("/scores")
	public boolean updateScores(@RequestBody BusinessActionContext actionContext,
								@RequestParam("accountCode") String accountCode,
								@RequestParam("frozenScores") Integer frozenScores) throws Exception {
		log.info("[updateScores] 收到更新账户信息请求, 用户:{}, 扣除积分:{}", accountCode, frozenScores);
		return accountAction.updateScoresPrepare(actionContext, accountCode, frozenScores);
	}
}
